package ay.shadow.fastbootweixin.controller;

import lombok.RequiredArgsConstructor;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author shadow
 */
@RestController
@RequestMapping("/shadowWx")
@RequiredArgsConstructor
public class WxTokenController {

    @RequestMapping("/dy")
    public Object getDy(String appid, String secret, String nonce, String echostr){
        // 第三方接口的URL
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
        if (appid!=null){
            url=url+"&appid="+appid+"&secret="+secret;
        }else {
            url=url+"&appid=wxa04b74a81097de4f&secret=a939cc08492a862b6ad609dd04076da0";
        }
        // 创建可关闭的HTTP客户端
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建GET请求
            HttpGet httpGet = new HttpGet(url);
            // 执行GET请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                if (entity!= null) {
                    // 将响应实体内容转换为字符串
                    String responseBody = EntityUtils.toString(entity);
                    System.out.println(responseBody);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
